<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
</head>
<body>
<script>
	var $={
		ajax:function (options) {
			//底层是封装的XMLHttpRequest
			var xhr = new XMLHttpRequest();

			if(options.type.toLowerCase()==="get"){
				options.url += "?"+options.data;
				options.data = null;
			}
			xhr.open(options.type,options.url);
			//如果是post提交
			if(options.type.toLowerCase()==="post"){
				xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")
			}
			xhr.send(options.data);
			//接收数据
			xhr.onreadystatechange = function () {
				if(xhr.status ===200 && xhr.readyState===4){
					/*if(options.success){
						options.success(xhr.responseText)
					}*/
					options.success && options.success(xhr.responseText);
				}
			}
		}
	}

	$.ajax({
		url:"ajax.php",
		type:"get",
		data:"username=zhangsan",
		success:function (info) {
			console.log(info);
		}
	});
</script>
</body>
</html>